/**
 * Created by guoxiaohui on 2018/8/23.
 */
export function formatDate (date, fmt) {
  if(date=='- -'){
    return '- -';
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
};

function padLeftZero (str) {
  return ('00' + str).substr(str.length);
};

export function formatSeconds(value) {
  if(!value){
    return '0秒';
  }
  if(value=='- -'){
    return '- -';
  }
  var theTime = parseInt(value);// 秒
  var theTime1 = 0;// 分
  var theTime2 = 0;// 小时
  // alert(theTime);
  if(theTime > 60) {
      theTime1 = parseInt(theTime/60);
      theTime = parseInt(theTime%60);
      // alert(theTime1+"-"+theTime);
      if(theTime1 > 60) {
          theTime2 = parseInt(theTime1/60);
          theTime1 = parseInt(theTime1%60);
      }
  }
  var result = ""+parseInt(theTime)+"秒";
  if(theTime1 > 0) {
      result = ""+parseInt(theTime1)+"分"+result;
  }
  if(theTime2 > 0) {
      result = ""+parseInt(theTime2)+"小时"+result;
  }
  return result;
}

export function formatSecondsEn(value) {
  if(!value){
    return '0s';
  }
  if(value=='- -'){
    return '- -';
  }
  var theTime = parseInt(value);// 秒
  var theTime1 = 0;// 分
  var theTime2 = 0;// 小时
  // alert(theTime);
  if(theTime >= 60) {
      theTime1 = parseInt(theTime/60);
      theTime = parseInt(theTime%60);
      // alert(theTime1+"-"+theTime);
      if(theTime1 > 60) {
          theTime2 = parseInt(theTime1/60);
          theTime1 = parseInt(theTime1%60);
      }
  }
  var result = " "+parseInt(theTime)+"s";
  if(theTime1 >= 0) {
      result = " "+parseInt(theTime1)+"min"+result;
  }
  if(theTime2 >= 0) {
      result = ""+parseInt(theTime2)+"h"+result;
  }
  return result;
}

export function  getBeforeDays(n){
  var day1 = new Date();
      day1.setTime(day1.getTime()-24*60*60*1000*parseInt(n));
      var moth=day1.getMonth()+1;
      if(moth<10){
      moth='0'+moth
      }
      var day=day1.getDate();
      if(day<10){
        day='0'+day;
      }
     var s1 = day1.getFullYear()+"-" + moth + "-" +day;
     return s1;
}

export const getNowFormatDate=()=>{
  var date = new Date();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
      month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + '年' + month + '月' + strDate
          + "日"+"  "+ date.getHours() + '时' + date.getMinutes()
          + '分' + date.getSeconds()+'秒';
  return currentdate;
}
